use file_sys::{
    block_dev::BlockDevice, 
    fs::FileSystem, 
    file_disk::FileDisk,
    BLOCK_SIZE,
};

fn get_device() -> Box<dyn BlockDevice> {
    Box::new(FileDisk::new("./disk/image").unwrap())
}

#[test]
fn test_fs_create() {
    let total_block_size = 8 * 1024 * 1024 / BLOCK_SIZE;
    FileSystem::create(total_block_size as u32, 2, get_device());
}

#[test]
fn test_fs_inode() {
    let total_block_size = 8 * 1024 * 1024 / BLOCK_SIZE;
    FileSystem::create(total_block_size as u32, 2, get_device());

    let fs = FileSystem::open(get_device());
    let root = FileSystem::root_inode(&fs);
    assert!(root.find("hello.c").is_none());
    assert!(root.create_file("hello.c").is_some());
    let hello_c = root.find("hello.c").unwrap();

    let _bin = root.create_directory("bin").unwrap();
    let _hello_rs = root.create_file("hello.rs").unwrap();

    for name in root.ls().unwrap() {
        println!("{}", name);
    }

    let mut buf = [0u8; 512];
    assert_eq!(hello_c.read(0, &mut buf), 0);
    hello_c.write(0, include_bytes!("../content/hello.c"));
    hello_c.read(0, &mut buf);
    println!("{}", core::str::from_utf8(&buf).unwrap());

    let vfs_rs = root.create_directory("vfs.rs").unwrap();
    vfs_rs.write(0, include_bytes!("../src/vfs.rs"));
    let mut buf = [0u8; 40960];
    vfs_rs.read(0, &mut buf);
    println!("{}", core::str::from_utf8(&buf).unwrap());
}